package com.lqk.nio.filechannel;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel01 {
    public static void main(String[] args) throws Exception {
        String str="hello, 尚硅谷";
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\Progrom Files\\java8new\\netty\\file\\file01.txt");

        // 通过FileOutputStream获取对应的 FileChannel
        // 这个 fileChannel 真实类型是 FileChannelImpl
        FileChannel fileChannel = fileOutputStream.getChannel();

        // 创建一个缓冲区 ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        // 将 str 放入到 byteBuffer
        byteBuffer.put(str.getBytes());

        // 切换Buffer的读写模式
        byteBuffer.flip();

        // 将byteBuffer数据写入到fileChannel
        fileChannel.write(byteBuffer);

        fileOutputStream.close();
        fileChannel.close();


    }
}
